<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<% String contextPath = request.getContextPath(); %>
<%@page import="common.*" %>
<%@ page import="java.util.*"%>

<%
	request.setCharacterEncoding("UTF-8");
	Customer user = (Customer)session.getAttribute("user");
	
	//サーブレットから遷移
	List<Item> itemlist = (List<Item>)request.getAttribute("itemlist");
	
	String category = (String)request.getAttribute("cg");
	String cg = null;
	//商品名検索の場合カテゴリは空
	if(category == null || category.length() == 0 || category.equals("null")) {
		category = "";
		 }
	else{ 
		cg = category;
		String c = Category.find_by_code(category);
		category = "カテゴリ　：　" + c; 
	}
	String itemname = (String)request.getAttribute("in");
	String in = null;
	//カテゴリ検索の場合商品名は空
	if(itemname == null || itemname.length() == 0 || itemname.equals("null")) {
		itemname = ""; 
		}
	else{ 
		in = itemname;
		itemname = "検索名　：　" + itemname;
		}
	if(!itemname.equals("") || itemname.length() != 0) category = "";
	//以上がサーブレットから遷移された場合
	
	//リンクから戻った場合はアトリビュートではなくパラメータなのでitemlistは取得できていない
	if(itemlist == null || itemlist.size() == 0)	
	{
		in = new String (request.getParameter("in").getBytes("UTF-8"));
		if(in != null && !in.equals("null") )
		{
			itemlist = Item.find_by_name(in);
			itemname = "検索名　：　" + in;
		}
		else{
			cg =  request.getParameter("cg");
			itemlist = Item.find_by_category(cg);
			String c = Category.find_by_code(cg);
			category = "カテゴリ　：　" + c;
		}
	}
	
	String msg = (String)request.getAttribute("msg");
	if(msg == null) msg = "";
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
		<link href="<%= contextPath %>/css/style.css" rel="stylesheet" type="text/css" />
		<link href="<%= contextPath %>/css/sidemenu.css" rel="stylesheet" type="text/css" />
		<link href="<%= contextPath %>/css/msg.css" rel="stylesheet" type="text/css" />
		<title>商品一覧</title>
	</head>
	<body>
		<div id="wrapper">
			<div id="sidemenu">
				<jsp:include page="sidemenu.jsp" flush="true"/>
			</div>
			<div id="contents">
				<div id="msg">
					<div id="search">
						<%= category %>
						<%= itemname %>
					</div>
					<div id="error">
						<%= msg %>
					</div>
					<div id="clear"></div>
				</div>
					<table border="1">
						<tr>
							<th>商品名</th>
							<th>規格</th>
							<th>情報</th>
							<th>売価</th>
							<th>在庫</th>
							<th>個数</th>
						</tr>
						<% for(int i = 0; i < itemlist.size(); i++){
							Item item = itemlist.get(i);%>
						<tr>
							<td><a href="../customer/itemInfo.jsp?code=<%= item.getCode() %>&in=<%= in %>&cg=<%= cg %>"><%= item.getShortName() %></a></td>
							<td><%= item.getSize() %></td>
							<td><%= item.getShortInfo() %></td>
							<td><%= item.getPrice() %>円</td>
							<td><%= item.getStock() %></td>
							<td><form action="../orderCus/OrderRegistServlet" method="post">
									<input type="text" name="orders" size="2"/><input type="hidden" name="code" value="<%= item.getCode() %>" />
									<input type="hidden" name="in" value="<%= in %>" />
									<input type="hidden" name="cg" value="<%= cg %>" />
									<input type="submit" value="カート" /></form></td>
						<tr>
						<%} %>
					</table>
				
			</div>
		</div>
	</body>
</html>
